package keeto.com.vn.bms.ui.activity;

import java.util.ArrayList;

import keeto.com.vn.bms.config.BMSCore;
import keeto.com.vn.bms.model.CommentModel;
import keeto.com.vn.bms.model.ListSurgeryModel;
import keeto.com.vn.bms.network.IBMSProtocol;
import keeto.com.vn.bms.ui.adapter.ListCommentAdapter;
import keeto.com.vn.bms.utils.BMSListener;
import keeto.com.vn.bms.utils.BMSSharedPreferences;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

public class ViewAllCommentActivity extends Activity implements OnClickListener, OnRefreshListener<ListView>{
	private ImageButton btnTittleBack;
	private TextView tvTitleName;
	private ImageButton btnTittleLogin;
	private ImageButton btnTittleDelete;
	private PullToRefreshListView lstProduct;
	private ListCommentAdapter listCommentAdapter;
	private ListSurgeryModel itemSurgeryModel;
	private String Schedule_id;
	int pageNumber = 0;
	public static BMSSharedPreferences preferences;
	ViewAllCommentListener mlistener = new ViewAllCommentListener();
	private static String ACTION_ITEM = "action_item_sergery";
	private static String ACTION_ID = "action_ID_sergery";
	
	public static void actionLaunch(Context context, ListSurgeryModel itemSurgeryModel) {
		Intent intent = new Intent(context, ViewAllCommentActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		intent.putExtra("ListSurgeryModel", itemSurgeryModel);
		intent.setAction(ACTION_ITEM);
		context.startActivity(intent);
	}
	public static void actionLaunchID(Context context, String ID) {
		Intent intent = new Intent(context, ViewAllCommentActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		intent.putExtra("ListSurgeryModelID", ID);
		intent.setAction(ACTION_ID);
		context.startActivity(intent);
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_list_infor);
		preferences = new BMSSharedPreferences(ViewAllCommentActivity.this);
		initView();
		onNewIntent(getIntent());
	}
	
	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
		String action = intent.getAction();
		Bundle bundle = intent.getExtras();
		if(bundle != null){
			if(action.equals(ACTION_ITEM)){
				itemSurgeryModel = (ListSurgeryModel) bundle.get("ListSurgeryModel");
				Schedule_id = itemSurgeryModel.getSurgeryId();
				BMSCore.getInstance(ViewAllCommentActivity.this).taskGetComment(preferences.getUserAuthKey(ViewAllCommentActivity.this), Schedule_id, "10", pageNumber+"", mlistener);
			} else if(action.equals(ACTION_ID)){
				Schedule_id = bundle.getString("ListSurgeryModelID");
				BMSCore.getInstance(ViewAllCommentActivity.this).taskGetComment(preferences.getUserAuthKey(ViewAllCommentActivity.this), Schedule_id, "10", pageNumber+"", mlistener);
			}
			
		}
	}

	private void initView() {
		btnTittleBack = (ImageButton) findViewById(R.id.btn_tittle_back);
		btnTittleBack.setOnClickListener(this);
		btnTittleLogin = (ImageButton) findViewById(R.id.btn_tittle_login);
		btnTittleLogin.setOnClickListener(this);
		btnTittleLogin.setVisibility(View.GONE);
		btnTittleDelete = (ImageButton) findViewById(R.id.btn_tittle_delete);
		btnTittleDelete.setOnClickListener(this);
		btnTittleDelete.setVisibility(View.GONE);
		tvTitleName = (TextView) findViewById(R.id.tv_title_name);
		tvTitleName.setText(ViewAllCommentActivity.this.getString(R.string.list_comment_sergery));
		lstProduct = (PullToRefreshListView) findViewById(R.id.lst_product);
		lstProduct.setOnRefreshListener(this);
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
		lp.setMargins(10, 0, 10, 0);
		lstProduct.setLayoutParams(lp);
		lstProduct.setOnScrollListener(new OnScrollListener() {
			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				BMSCore.getInstance(ViewAllCommentActivity.this).taskGetComment(preferences.getUserAuthKey(ViewAllCommentActivity.this), Schedule_id, "10", pageNumber+"", mlistener);
			}
			@Override
			public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_tittle_back:
			finish();
			break;
		default:
			break;
		}
	}

	@Override
	public void onRefresh(PullToRefreshBase<ListView> refreshView) {
		BMSCore.getInstance(ViewAllCommentActivity.this).taskGetComment(preferences.getUserAuthKey(ViewAllCommentActivity.this), Schedule_id, "10", "10", mlistener);
		new Handler().post(new Runnable() {
			@Override
			public void run() {
				lstProduct.onRefreshComplete();
			}
		});
	}
	
	class ViewAllCommentListener extends BMSListener{
		@Override
		public void finishGetComment(final int returnCode, String resultMsg, int recordNumber, int countPage, final ArrayList<CommentModel> listComment) {
			super.finishGetComment(returnCode, resultMsg, recordNumber, countPage, listComment);
			runOnUiThread(new Runnable() {
				@Override
				public void run() {
					if (returnCode == IBMSProtocol.STATUS_CODE_OK){ 
						pageNumber++;
						listCommentAdapter = new ListCommentAdapter(ViewAllCommentActivity.this, listComment);
						lstProduct.setAdapter(listCommentAdapter);
						lstProduct.onRefreshComplete();
					}else if (returnCode == IBMSProtocol.STATUS_CODE_GENERIC_FAIL){
					}else if (returnCode == IBMSProtocol.STATUS_CODE_GENERIC_FAIL_CONECTTION){
						Toast.makeText(ViewAllCommentActivity.this, ViewAllCommentActivity.this.getResources().getString(R.string.error_network_connection), Toast.LENGTH_SHORT).show();
					}else if(returnCode==IBMSProtocol.STATUS_CODE_EXIT_RECORD){
						Toast.makeText(ViewAllCommentActivity.this, ViewAllCommentActivity.this.getResources().getString(R.string.error_data_server), Toast.LENGTH_SHORT).show();
					}
				}
			});
			
			
		}
	}
	
}
